<?php

  include ('const.php');
  
  // Lesen des Dateinamens aus der fileId
  // ---
  include ('dbsettings.php');
  $query = "SELECT * FROM document WHERE id=". $_REQUEST['fileId'];
  if ($result = $mysqli->query($query)) {
    $row = $result->fetch_object();
    
    // Streaming der Datei aus dem Unterverzeichnis $target
    // ---
    $fileDownloadName = $target . $row->filename;
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($fileDownloadName));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize(($fileDownloadName)));
    header('Connection: Close');
    readfile($fileDownloadName);
    $result->close();
  }
  $mysqli->close();
  exit();
?>
